﻿using System;
using ArtMath.Data;
using ArtMath.Core.Data;
using ArtMath.Resource;
using ArtMath.Attribute;

namespace ArtMath.Component.Point
{
    /// <summary>
    /// 直角坐标系点基类
    /// </summary>
    [Serializable]
    public abstract class RPointBase : PointBase
    {
        public RPointBase(SketchPad pad, string name, ToolTypeName toolType, bool addToList = false) : base(pad, name, toolType, addToList)
        {
            CoordinateDisplayer = new PropertyDisplayer(this, Resources.Translate("Coordinate"), "({0}, {1})", "X", "Y");
        }
        [MultilingualCategory("Structure")]
        public override PadStringX X
        {
            get
            {
                return base.X;
            }

            set
            {
                if (X is object)
                {
                    X.ParametersValueChanging -= XY_ParametersValueChanging;
                    X.ParametersValueChanged -= XY_ParametersValueChanged;
                    X.ExpressionSet -= XY_ExpressionSet;
                    X.ExpressionSetting -= XY_ExpressionSetting;
                }
                base.X = value;
                if (X is object)
                {
                    X.ParametersValueChanging += XY_ParametersValueChanging;
                    X.ParametersValueChanged += XY_ParametersValueChanged;
                    X.ExpressionSet += XY_ExpressionSet;
                    X.ExpressionSetting += XY_ExpressionSetting;
                }
            }
        }
        [MultilingualCategory("Structure")]
        public override PadStringX Y
        {
            get
            {
                return base.Y;
            }

            set
            {
                if (Y is object)
                {
                    Y.ParametersValueChanging -= XY_ParametersValueChanging;
                    Y.ParametersValueChanged -= XY_ParametersValueChanged;
                    Y.ExpressionSet -= XY_ExpressionSet;
                    Y.ExpressionSetting -= XY_ExpressionSetting;
                }
                base.Y = value;
                if (Y is object)
                {
                    Y.ParametersValueChanging += XY_ParametersValueChanging;
                    Y.ParametersValueChanged += XY_ParametersValueChanged;
                    Y.ExpressionSet += XY_ExpressionSet;
                    Y.ExpressionSetting += XY_ExpressionSetting;
                }
            }
        }
        void XY_ExpressionSetting(StringX sender)
        {
            Pad.MoveObject_DoMouseDown(this);
        }
        void XY_ParametersValueChanging(DocStringX sender)
        {
            Pad.MoveObject_DoMouseDown(this);
        }
        void XY_ParametersValueChanged(DocStringX sender, ParametersValueChangedEventArgs e)
        {
            if (Pad.MoveObject_DoMouseMove(this, Pad.XYToPxy(ToPointD())))
            {

            }
            Pad.MoveObject_DoMouseUp(this);
        }
        void XY_ExpressionSet(StringX sender, ExpressionSetEventArgs e)
        {
            if (Pad.MoveObject_DoMouseMove(this, Pad.XYToPxy(ToPointD())))
            {

            }
            else
            {
                Pad.ShowMessage(string.Format(Resources.Translate("FailedModify"), Resources.Translate("Coordinate")), MessageType.Exclamation);
                sender.SetExpression(e.OldExpression, false);
            }
            Pad.MoveObject_DoMouseUp(this);
        }
        public override bool CoordinateExpressionContainParameter
        {
            get
            {
                return X.ContainsParameter || Y.ContainsParameter;
            }
        }
    }
}
